package com.igeek.compus.backend.seller.controller;

import com.igeek.compus.backend.user.controller.UserController;
import com.github.pagehelper.PageInfo;
import com.igeek.compus.backend.seller.entity.Seller;
import com.igeek.compus.backend.seller.service.SellerService;
import com.igeek.compus.backend.system.Constants;
import com.igeek.compus.backend.user.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("seller")
public class SellerController {

    @Autowired
    private SellerService sellerService;

    /**
     * 获得卖家列表
     *
     * @param seller
     * @return
     */
    @RequestMapping("list.do")
    @ResponseBody
    public Map<String,Object> list(Seller seller) {
        Map<String,Object> result = new HashMap<>();

        try {
            PageInfo<Seller> pageInfo = new PageInfo<>(sellerService.getSellerListByPage(seller));

            result.put(Constants.RETDATA, pageInfo.getList());
            result.put(Constants.COUNT, pageInfo.getTotal());

            result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
            result.put(Constants.RETMSG, Constants.SUCCESS_MSG);
        } catch (Exception e) {
            e.printStackTrace();
            result.put(Constants.RETCODE, Constants.ERROR_CODE);
            result.put(Constants.RETMSG, Constants.ERROR_MSG);
        }
        return result;
    }

    // 通过id删除卖家信息
    @RequestMapping("deleteSellerById.do")
    @ResponseBody
    public void deleteSellerById(Seller seller) {
        System.out.println(seller);
        sellerService.deleteSellerById(seller);
    }

    // 批量删除卖家信息
    @RequestMapping("deleteSellerList.do")
    @ResponseBody
    public Map<String, Object> deleteSellerList(String sellerIdList) {
        Map<String, Object> result = new HashMap<>();

        List<String> sellerIds = Arrays.asList(sellerIdList.substring(1, sellerIdList.length() - 1).split(","));

        // 执行业务
        try {
            if (sellerService.deleteSellerList(sellerIds) > 0) {
                result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
                result.put(Constants.RETMSG, Constants.SUCCESS_MSG);
            } else {
                result.put(Constants.RETCODE, Constants.ERROR_CODE);
                result.put(Constants.RETMSG, Constants.SUCCESS_MSG);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
            result.put(Constants.RETMSG, Constants.ERROR_MSG);
        }

        // 返回结果
        return result;
    }

    // 编辑卖家信息
    @RequestMapping("toSellerEdit.do")
    @ResponseBody
    public ModelAndView toSellerEdit(Integer id) {
        Map<String, Object> map = new HashMap<>();

        Seller seller=new Seller();
        seller.setId(id);
        List<Seller> sellerList1 = sellerService.getSellerList(seller);

        map.put("userId",sellerList1.get(0).getUserId());
        map.put("nickName",sellerList1.get(0).getNickName());
        map.put("phone",sellerList1.get(0).getPhone());
        map.put("selledCount",sellerList1.get(0).getSelledCount());
        map.put("sellingCount",sellerList1.get(0).getSellingCount());
        return new ModelAndView("/seller/seller_edit", map);
    }

    // 更新卖家信息
    @RequestMapping("updateSeller.do")
    @ResponseBody
    public Map<String, Object> updateSeller(Seller seller) {
        Map<String, Object> result = new HashMap<>();

        // 参数校验
        if (seller.getId() == null) {
            result.put(Constants.RETCODE, Constants.ERROR_CODE);
            result.put(Constants.RETMSG, Constants.PARAM_ERROR_MSG);
            return result;
        }

        // 执行业务
        try {
            if (sellerService.updateSeller(seller) > 0) {
                result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
                result.put(Constants.RETMSG, Constants.SUCCESS_MSG);
            } else {
                result.put(Constants.RETCODE, Constants.ERROR_CODE);
                result.put(Constants.RETMSG, Constants.SUCCESS_MSG);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put(Constants.RETCODE, Constants.SUCCESS_CODE);
            result.put(Constants.RETMSG, Constants.ERROR_MSG);
        }

        // 返回结果
        return result;
    }
}
